package com.winit.openapi.wh.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.winit.oms.spi.inbound.InboundCustomsDataFileService;
import com.winit.oms.spi.inbound.command.InboundCustomsDataFileCommand;
import com.winit.oms.spi.inbound.vo.InboundCustomsDataFileVo;
import com.winit.openapi.controller.BaseController;
import com.winit.openapi.model.RequestMsg;
import com.winit.openapi.model.ResponseMsg;
import com.winit.openapi.util.ValidateUtil;
import com.winit.openapi.wh.builder.InboundCustomsDataFileBuilder;

/**
 * 入库单进出口报关文件
 * 
 * @version <pre>
 * Author	Version		Date		Changes
 * ming.chen 	1.0  		2017-5-18 	Created
 * 
 * </pre>
 * @since 1.
 */
@Controller
@RequestMapping(value = "/wh/customsDataFile")
public class InboundCustomsDataFileConroller extends BaseController {

    @Resource
    private InboundCustomsDataFileService inboundCustomsDataFileService;

    /**
     * 新增
     * 
     * @param requestMsg
     * @param responseMsg
     * @return
     */
    @RequestMapping(value = "/addCustomsDataFileList")
    public String addInboundCustomsDataFileList(@ModelAttribute RequestMsg requestMsg,
                                                @ModelAttribute ResponseMsg responseMsg) {
        InboundCustomsDataFileCommand command = new InboundCustomsDataFileCommand();
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "orderNo");
            command.setWinitOrderNo(json.getString("orderNo"));
            command.setFileUrl(json.getString("fileUrl"));
            command.setFileName(json.getString("fileName"));
            command.setCustomsDataType(json.getString("customsDataType"));

        }
        InboundCustomsDataFileVo vo = inboundCustomsDataFileService.addInboundCustomsDataFile(command);
        
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("inboundCustomsDataFileVo", InboundCustomsDataFileBuilder.buildInboundCustomsDataFileMap(vo));
        responseMsg.setData(data);
        return SUCCESS;
    }

    @RequestMapping(value = "/deleteFileById")
    public String deleteInboundCustomsDataFileById(@ModelAttribute RequestMsg requestMsg,
                                                   @ModelAttribute ResponseMsg responseMsg) {
        InboundCustomsDataFileCommand command = new InboundCustomsDataFileCommand();
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "orderNo");
            ValidateUtil.validNotNull(json, "id");
            command.setWinitOrderNo(json.getString("orderNo"));
            if (json.containsKey("id")) {
                command.setId(json.getLong("id"));
            }
        }

        String orderNo = inboundCustomsDataFileService.deleteInboundCustomsDataFileById(command);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("orderNo", orderNo);
        responseMsg.setData(data);
        return SUCCESS;
    }

    @RequestMapping(value = "/deleteFileByUrl")
    public String deleteInboundCustomsDataFileByCustomsDataUrl(@ModelAttribute RequestMsg requestMsg,
                                                               @ModelAttribute ResponseMsg responseMsg) {
        InboundCustomsDataFileCommand command = new InboundCustomsDataFileCommand();
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "orderNo");
            ValidateUtil.validNotNull(json, "customsDataUrl");
            command.setWinitOrderNo(json.getString("orderNo"));
            command.setId(json.getLong("customsDataUrl"));
        }

        String orderNo = inboundCustomsDataFileService.deleteInboundCustomsDataFileByCustomsDataUrl(command);
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("orderNo", orderNo);
        responseMsg.setData(data);
        return SUCCESS;
    }

    @RequestMapping(value = "/queryByOrderNo")
    public String queryByWinitOrderNo(@ModelAttribute RequestMsg requestMsg, @ModelAttribute ResponseMsg responseMsg) {
        InboundCustomsDataFileCommand command = new InboundCustomsDataFileCommand();
        Object obj = requestMsg.getData();
        if (obj instanceof JSONObject) {
            JSONObject json = (JSONObject) obj;
            ValidateUtil.validNotNull(json, "orderNo");
            command.setWinitOrderNo(json.getString("orderNo"));
        }
        List<InboundCustomsDataFileVo> list = inboundCustomsDataFileService.queryByWinitOrderNo(command);

        List<Map<String, Object>> rslist = new ArrayList<Map<String, Object>>();
        Map<String, Object> map = null;
        for (InboundCustomsDataFileVo vo : list) {
            map = InboundCustomsDataFileBuilder.buildInboundCustomsDataFileMap(vo);
            rslist.add(map);
        }
        Map<String, Object> data = new HashMap<String, Object>();
        data.put("list", rslist);
        responseMsg.setData(data);
        return SUCCESS;
    }
}
